package com.suntown;

import com.suntown.util.TestMySQL;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

public class Main extends Application {
    public static void main(String[] args){
        launch(args);
    }

    private void initView(Parent root, Stage primaryStage) {
        final TextField txtUser = (TextField) root.lookup("#txtUser");
        final TextField txtPwd = (TextField) root.lookup("#txtPwd");

        final Button btnStart = (Button) root.lookup("#btnStart");
        btnStart.setOnMouseClicked(new EventHandler<MouseEvent>(){

            public void handle(MouseEvent mouseEvent) {
                btnStart.setDisable(true);
                int iresult = TestMySQL.execute(txtUser.getText(),txtPwd.getText());
                if(iresult == 0){
                    new Alert(Alert.AlertType.ERROR,"连接失败,请检查用户名密码").show();
                }else if(iresult == -1){
                    new Alert(Alert.AlertType.ERROR,"连接失败,数据库服务未启动或已停止").show();
                }else{
                    new Alert(Alert.AlertType.ERROR,"更新完成,登录密码为123").show();
                }
                btnStart.setDisable(false);
            }
        });

    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/main.fxml"));
        initView(root,primaryStage);
        primaryStage.setTitle("档案系统密码重置");
        Scene scene = new Scene(root, 630, 350);
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
